﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CardID
{
    public partial class requestControl : UserControl
    {
        string IDcard;
        string amount;
        public requestControl()
        {
            InitializeComponent();
            this.activeCodeT.Enabled = true;
            this.Name = "requestControl";
            this.Visible = false;
            this.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string activateCode;
            if (activeCodeT.Text != null && activeCodeT.Text != "")
            {
                
                activateCode =  activeCodeT.Text;
                message.Text = "Waitting for request....";
                localhost.MakePaymentResponse makeResponse = null;
                while (makeResponse == null)
                {
                    Thread.Sleep(500);
                    makeResponse = ServiceClient.sendRequest();
                }
                // check response and send information
                if (makeResponse.ack)
                {
                    try
                    {
                        message.Text = "Waitting...We will authenticate your Activation Code";
                        localhost.FinishPaymentResponse executeResponse = null;
                        while (executeResponse == null)
                        {
                            Thread.Sleep(300);
                            executeResponse = ServiceClient.finishRequest(activateCode,2323);
                        }
                        if (executeResponse.ack)
                        {
                            changeControlTo("endControl");
                        }
                        else
                        {
                            MessageBox.Show("Request to execute failed.Please try again", "Infomation transaction", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Infomation transaction", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
                else
                {
                    MessageBox.Show("Request failed.Please try again", "Infomation transaction", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
            }
            else
            {
                MessageBox.Show("Active code can not null.Please input", "ActiveCode", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }
        public string IDCard
        {
            get{
                return IDcard;
            }
            set{
                IDcard = value;
            }
        }
        public string amountProperty
        {
            get
            {
                return amount;
            }
            set
            {
                amount = value;
            }
        }
        public void changeControlTo(string nextNameControl)
        {
            this.Visible = false;
            this.Enabled = false;
            Control[] list = this.Parent.Controls.Find(nextNameControl, true);
            Control result = null;
            //pass data to request
            //send request to Service

            if (list.Length != 0)
            {
                result = list[0];
                result.Visible = true;
                result.Enabled = true;
            }
        }
    }
}
